package org.jeecg.modules.flow.callback.delivery.move;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.RequiredArgsConstructor;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.modules.business.delivery.entity.WmsDeliveryTask;
import org.jeecg.modules.business.delivery.service.IWmsDeliveryTaskService;
import org.jeecg.modules.business.third.entity.EmsOrDmsDeliveryDto;
import org.jeecg.modules.business.third.service.IEmsOrDmsDeliveryOperateService;
import org.jeecg.modules.business.transfer.entity.WmsMoveReceipt;
import org.jeecg.modules.business.transfer.service.IWmsMoveReceiptService;
import org.jeecg.modules.business.warehouse.entity.WmsInReceiptTask;
import org.jeecg.modules.business.warehouse.service.IWmsInReceiptTaskService;
import org.jeecg.modules.flow.sys.common.BaseCustomBeanRun;
import org.jeecg.modules.flow.sys.entity.dto.BaseFlowDto;
import org.jeecg.modules.flow.sys.service.WorkFlowService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * 条料调拨因入移库单 通知推料架
 *
 * @author gongwenkai
 * @version 1.0.0
 * @since 2023-11-27
 */
@RequiredArgsConstructor
@Component
public class Y103MoveReceiptNoticeBackBean<T> extends BaseCustomBeanRun<T> {

    private final IWmsMoveReceiptService moveReceiptService;

    private final IWmsDeliveryTaskService deliveryTaskService;

    private final IWmsInReceiptTaskService inReceiptTaskService;

    private final WorkFlowService workFlowService;

    private final IEmsOrDmsDeliveryOperateService emsOrDmsDeliveryOperateService;

    @Override
    public void actureRun() {
        BaseFlowDto baseFlowDto = this.getBaseFlowDto();
        String receiptId = baseFlowDto.getInfoId();
        // 主动调用MDS发送可退空料架请求
        EmsOrDmsDeliveryDto emsOrDmsDeliveryDto =new EmsOrDmsDeliveryDto();
        emsOrDmsDeliveryDto.setReceiptId(receiptId);
        emsOrDmsDeliveryDto.setTaskOrReciptType(2);
        emsOrDmsDeliveryDto.setType(2);
        emsOrDmsDeliveryOperateService.move(emsOrDmsDeliveryDto);

        // 推进入库任务节点
        WmsMoveReceipt moveReceipt = moveReceiptService.getById(receiptId);
        if(Objects.isNull(moveReceipt)){
            throw new JeecgBootException("移库单不存在");
        }
        if(!moveReceipt.getDocumentType().equals("Y103")){
            throw new JeecgBootException("单据类型错误");
        }
//        String deliveryTaskId = moveReceipt.getDeliveryTaskId();
//        WmsDeliveryTask deliveryTask = deliveryTaskService.getById(deliveryTaskId);
//        if(Objects.isNull(deliveryTask)){
//            throw new JeecgBootException("调拨任务不存在");
//        }
//        String inTaskCode = deliveryTask.getInTaskCode();
//        WmsInReceiptTask inReceiptTask = inReceiptTaskService.getOne(new LambdaQueryWrapper<WmsInReceiptTask>()
//                .eq(WmsInReceiptTask::getDocumentCode, inTaskCode)
//        );
//        if(Objects.isNull(inReceiptTask )){
//            throw new JeecgBootException("调拨入库任务不存在");
//        }
//        workFlowService.backAutoCommit(inReceiptTask.getId(),null);
        baseFlowDto.setInfoId(receiptId);
        baseFlowDto.setResult(Result.OK());
    }
}
